<?php
/**
 * To run this example, just input the relevant MySQL connection info.
 * Further customizing of this example is simple:
 * Replace the includes for "login.php", "logout.php" and "myPage.php" with your own files.
 * Even further customizing is simple too:
 * Just have a look at the documentation.
 *
 * @package    PageBricks
 * @subpackage Xamples
 * @version Auth_r12+
 * @filesource
 */
/**
 * 
 */

//The bootstrap's task is to make the PageBricks folder available by adding its parent folder to your include path.
require_once 'bootstrap.php';
//The autoLoader makes it possible to use classes from PageBricks without having to first include/require them.
require_once 'PageBricks/autoLoader.php';

//Your DB info goes here.
$dbHost = '';
$dbUser = '';
$dbPwd = '';
$dbDb = '';
//Place info neatly into an array.
$dbParams = array($dbHost, $dbUser, $dbPwd, $dbDb);

/**Required funtion to handle the login.
 * 
 * This function will get called by PageBricks_Auth. Just declare a function with one argument exactly as below.
 * 
 * @params PageBricks_Auth $auth
 */
function loginFunction($auth = null)
{
	//Place code/include for a login page here.
	include 'login_simple.html';
}

/**Optional function as one way to react to a logout.
 * 
 * This will get called by PageBricks_Auth.
 * 
 * @params PageBricks_Auth $auth
 */
function logoutFunction($auth = null)
{
	//Place code/include for logout handling here.
	echo 'logged out successfully <br />';
	echo '<a href="' . $auth->getScriptName() . '">log back in<a>';
}

/**
 * Optional function if you work with password encryption.
 * @param $pwd
 * @param $aut
 * @return unknown_type
 */
function encryptionFunction($pwd = '', $salt = '', $aut = null)
{
	//Place code/include for pwd encryption here:
	return sha1(md5($pwd . $salt));
}

//Instantiate a PageBricks_Auth object.
$a = new PageBricks_Auth($dbParams, 'loginFunction');

//Tell your PageBricks_Auth the name of your logout function.
$a->setLogoutFunction('logoutFunction');

//Tell your PageBricks_Auth object the name of the encryption function.
$a->setEncryptionFunction('encryptionFunction');

//Check if the user has been authenticated and logged in. If not exit and display minimal response,
//otherwise proceed to show your content.
if ($a->authenticate() !== true) {
	echo '<br/>could not authenticate user';
	exit;
}
//Your content goes here.
include 'myPage.php';
?>